package com.k.base.jdk.tree.treecov;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 树形数据转换
 * <link>https://blog.csdn.net/qq_21454973/article/details/87854945</link>
 * </p>
 *
 * @author kivil
 */
public class TreeTest {

    /**
     * 简单结构树 转换 标准结构树
     * SimpleTree -> StdTree
     * @param simpleTreeList Simple tree
     * @return Standard tree
     */
    public static List<Tree> getTreeStd(List<Tree> simpleTreeList){
        List<Tree> stdTreeList = new ArrayList<Tree>();
        // 找 根节点
        for(Tree itm:simpleTreeList){
            if("0".equals(itm.getPid())){
                stdTreeList.add(itm);
            }
        }
        // 找 子节点
        findChildren(stdTreeList,simpleTreeList);
        System.out.println(stdTreeList);
        return stdTreeList;
    }

    private static void findChildren(List<Tree> stdTreeList, List<Tree> simpleTreeList) {
        for(Tree stdTree: stdTreeList){
            List<Tree> children = new ArrayList<>();
            for(Tree simTree:simpleTreeList){
                if(stdTree.getId()!=null && stdTree.getId().equals(simTree.getPid())){
                    children.add(simTree);
                }
            }
            stdTree.setChildren(children);
            findChildren(children,simpleTreeList);
        }
    }

    public static void main(String[] args) {
        List<Tree> simpTree = new ArrayList<>();
        Tree tree1 = new Tree();
        tree1.setId("1");
        tree1.setName("ljw1");
        tree1.setPid("0");
        simpTree.add(tree1);

        Tree tree11 = new Tree();
        tree11.setId("2");
        tree11.setName("ljw2");
        tree11.setPid("1");
        simpTree.add(tree11);

        Tree tree111 = new Tree();
        tree111.setId("111");
        tree111.setName("ljw111");
        tree111.setPid("2");
        simpTree.add(tree111);

        List<Tree> stdTree = getTreeStd(simpTree);
        System.out.println();


    }

    /**
     * 对集合进行深拷贝
     * @param src src list
     * @param <T> 注意需要岁泛型类进行序列化（实现serializable）
     * @return result
     */
    public static <T> List<T> deepCopy(List<T> src) {
        List<T> dest = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            dest = (List<T>) in.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        return dest;
    }
    /**
     * 对集合进行深拷贝
     * 注意需要岁泛型类进行序列化（实现serializable）
     *
     * @param src src list
     * @param <T> 注意需要岁泛型类进行序列化（实现serializable）
     * @return result
     */
    public static <T> List<T> deepCopy2(List<T> src) {
        try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
             ObjectOutputStream outputStream = new ObjectOutputStream(byteOut);
        ) {
            outputStream.writeObject(src);
            try (ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                 ObjectInputStream inputStream = new ObjectInputStream(byteIn);
            ) {
                return (List<T>) inputStream.readObject();
            }catch (Exception e){
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}
